{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b975f090",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e:\\shuyang\\代码文件\\Python训练营\\day30演示案例\\使用场景4\\model\n",
      "d:\\Anaconda\\envs\\vs\\python38.zip\n",
      "d:\\Anaconda\\envs\\vs\\DLLs\n",
      "d:\\Anaconda\\envs\\vs\\lib\n",
      "d:\\Anaconda\\envs\\vs\n",
      "\n",
      "d:\\Anaconda\\envs\\vs\\lib\\site-packages\n",
      "d:\\Anaconda\\envs\\vs\\lib\\site-packages\\win32\n",
      "d:\\Anaconda\\envs\\vs\\lib\\site-packages\\win32\\lib\n",
      "d:\\Anaconda\\envs\\vs\\lib\\site-packages\\Pythonwin\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "for i in sys.path:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37acc080",
   "metadata": {},
   "source": [
    "python解释器会自动在sys.path中的目录中搜索模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "744dff1f",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'utils'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m circle \u001b[38;5;66;03m# 这是根目录绝对路径的导入方式\u001b[39;00m\n\u001b[0;32m      3\u001b[0m radius \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n\u001b[0;32m      4\u001b[0m area \u001b[38;5;241m=\u001b[39m circle\u001b[38;5;241m.\u001b[39mcalculate_area(radius)\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'utils'"
     ]
    }
   ],
   "source": [
    "from utils import circle # 这是根目录绝对路径的导入方式\n",
    "\n",
    "radius = 5\n",
    "area = circle.calculate_area(radius)\n",
    "print(f\"半径为 {radius} 的圆，面积是: {area}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b23ae7cf",
   "metadata": {},
   "source": [
    "因为utils并不在model这个路径下，所以直接运行会报错\n",
    "\n",
    "有2类策略：\n",
    "1. 把utils路径加入到sys.path中\n",
    "2. 在py文件的终端中直接用python -m model.main 运行，这种导入方法会默认从根目录开始查找。\n",
    "\n",
    "ps：之所以你会感觉终端的命令明明是根目录，但是运行时却找不到，是因为python解释器理解的目录和终端的目录不一样，通过sys.path可以查看解释器理解的目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1c95e437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e:\\\\shuyang\\\\代码文件\\\\Python训练营\\\\day30演示案例\\\\使用场景4'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "\n",
    "project_root = os.path.abspath(os.path.join(os.getcwd(), '..'))\n",
    "project_root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f6a1651b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['e:\\\\shuyang\\\\代码文件\\\\Python训练营\\\\day30演示案例\\\\使用场景4',\n",
       " 'e:\\\\shuyang\\\\代码文件\\\\Python训练营\\\\day30演示案例\\\\使用场景4\\\\model',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\python38.zip',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\DLLs',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\lib',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs',\n",
       " '',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\lib\\\\site-packages',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\lib\\\\site-packages\\\\win32',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\lib\\\\site-packages\\\\win32\\\\lib',\n",
       " 'd:\\\\Anaconda\\\\envs\\\\vs\\\\lib\\\\site-packages\\\\Pythonwin']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果项目根目录不在 sys.path 中，则添加它 (通常添加到开头，优先搜索)\n",
    "if project_root not in sys.path:\n",
    "    sys.path.insert(0, project_root)\n",
    "sys.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1698c7d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "半径为 5 的圆，面积是: 78.53981633974483\n"
     ]
    }
   ],
   "source": [
    "from utils import circle # 这是根目录绝对路径的导入方式\n",
    "\n",
    "radius = 5\n",
    "area = circle.calculate_area(radius)\n",
    "print(f\"半径为 {radius} 的圆，面积是: {area}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba92e93",
   "metadata": {},
   "source": [
    "所以导入包的核心就是找到目录，只有理解了python解释器的目录关系，才能导入包，如果py文件中导入失败，不妨多调试几次路径即可-----一点心得，经常跑别人的项目都是在这里有问题"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
